package com.web.surf.sample.eshop.utils;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import com.web.surf.sample.eshop.faces.managed.beans.OrderBean;

public class ManagedBeanProvider {

	public static OrderBean getOrderBean() {
		FacesContext context = FacesContext.getCurrentInstance();
		ELContext elContext = context.getELContext();
		Application application = context.getApplication();
		ExpressionFactory expressionFactory = application
				.getExpressionFactory();
		ValueExpression ve = expressionFactory.createValueExpression(elContext,
				"#{orderBean}", OrderBean.class);
		OrderBean managedBean = (OrderBean) ve.getValue(elContext);
		if (managedBean != null) {
			return managedBean;
		} else
			return new OrderBean();
	}

}
